package com.blog.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.List;

public class UploadAction extends ActionSupport {
    /*上传文件的目录（相对于应用下的文件目录）*/
    private static String uploadDir = "/upload/a/b/c";

    /*文件对应的 File 对象 [fileFieldName],与页面标签 name 属性的值保持一致 */
    private List<File> app;

    /*文件类型 [fileFieldName]ContentType */
    private List<String> appContentType;

    /*文件名 [fileFieldName]FileName */
    private List<String> appFileName;

    public List<File> getApp() {
        return app;
    }

    public void setApp(List<File> app) {
        this.app = app;
    }

    public List<String> getAppContentType() {
        return appContentType;
    }

    public void setAppContentType(List<String> appContentType) {
        this.appContentType = appContentType;
    }

    public List<String> getAppFileName() {
        return appFileName;
    }

    public void setAppFileName(List<String> appFileName) {
        this.appFileName = appFileName;
    }

    @Override
    public String execute() throws Exception {
        String path = ServletActionContext.getServletContext().getRealPath(uploadDir);
        File parent = new File(path);
        for (int i = 0; i < app.size(); i++) {
            FileUtils.copyFile(app.get(i), new File(parent, appFileName.get(i)));
            //存入数据库字段的文件名称及路径
            String savePath = uploadDir+ File.separator + appFileName.get(i);
            // TODO: 2017/2/23 13:05 数据存入数据库的代码
        }
        return SUCCESS;
    }

}
